<?php
$host = 'localhost';
$port = 8081;
$errno = '';
$errstr = '';
$timeout = 30;
$url = '/TestServer.php';

$form_data = array(
    'name' => 'fdipzone',
    'gender' => 'man',
);

$file_data = array(
    array(
        'name' => 'ludashi',
        'filename' => 'ludashi.jpg',
        'path' =>'d:/ludashi.jpg'
    )
);

// create connect
$fp = fsockopen($host, $port, $errno, $errstr, $timeout);

if(!$fp){
    return false;
}

// send request
srand((double)microtime()*1000000);
$boundary = "---------------------------".substr(md5(rand(0,32000)),0,10);

$line_break = "\n";
$data = "--$boundary".$line_break;

// form data
foreach($form_data as $key=>$val){
    $data .= "Content-Disposition: form-data; name=\"".$key."\"".$line_break;
    $data .= "Content-type:text/plain".$line_break.$line_break;
    $data .= rawurlencode($val).$line_break;
    $data .= "--$boundary".$line_break;
}

// file data
foreach($file_data as $file){
    $data .= "Content-Disposition: form-data; name=\"".$file['name']."\"; filename=\"".$file['filename']."\"".$line_break;
    $data .= "Content-Type: image/jpeg".$line_break.$line_break;
    $data .= implode("",file($file['path']))."\r\n";
    //$data .= "aa".$line_break;
    $data .= "--$boundary".$line_break;
}

$data .="--".$line_break.$line_break;

//echo $data."<br>";

$out = "POST ${url} HTTP/1.1".$line_break;
$out .= "Host:${host}".$line_break;
$out .= "Content-type:multipart/form-data; boundary=$boundary".$line_break; // multipart/form-data
$out .= "Content-length:".strlen($data).$line_break.$line_break;
//$out .= "Connection:close".$line_break.$line_break;
$out .= "${data}";

fputs($fp, $out);

// get response
$response = '';
while($row=fread($fp, 4096)){
    $response .= $row;
}

fclose($fp);

$pos = strpos($response, "\r\n\r\n");
$response = substr($response, $pos+4);

echo $response;
?>